<?php

namespace App\Models\User;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class WxUserWithdrawal extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'wx_user_withdrawal';

    public function user()
    {
        return $this->hasOne(WxUser::class, "id", "user_id");
    }

    public function ower()
    {
        return $this->hasOne(WxUser::class, 'id', 'user_id');
    }


    public function normal($provider, $pay_type){
        if($provider == 'wxpay'){
            if($pay_type == 'app'){
                $wxopen_id = WxUser::where('id', $this->user_id)->value('weixin_app_openid');
                if(_empty_($wxopen_id)){
                    return null;
                }
            }else if($pay_type == 'mp'){
                $wxopen_id = WxUser::where('id', $this->user_id)->value('weixin_mp_openid');
                if(_empty_($wxopen_id)){
                    return null;
                }
            }else if($pay_type == 'mini'){
                $wxopen_id = WxUser::where('id', $this->user_id)->value('weixin_openid');
                if(_empty_($wxopen_id)){
                    return null;
                }
            }
            return [
                'out_detail_no' => $this->withdrawal_id,
                'transfer_amount' => (int)($this->pay_amount * 100),
                'transfer_remark' => _empty_default_($this->withdrawal_remark, '余额提现'.$this->pay_amount.'元到微信零钱'),
                'openid' => $wxopen_id
            ];
        }else if($provider == 'alipay'){
            return null;
        }

    }

}
